<?php
namespace app\shop\controller;

use x\Container;

class Order extends Base
{


    public function lists(){
        $pay_status = $this->get('pay_status');
        $fahuo_status = $this->get('fahuo_status');
        if(is_post()){
            $d = $this->post;
            $map = [];
            $map[] = ['and','shop_id = ?',[$this->userinfo['id']]];
            if($d['keyword']!=''){
              $map[] = ['and',"(a.order_no like ? or a.title like ?)",["%{$d['keyword']}%","%{$d['keyword']}%"]];
            }
            if($pay_status!='') $map[] = ['and','pay_status','=',$pay_status];
            if($fahuo_status!='') $map[] = ['and','fahuo_status','=',$fahuo_status];

            if($d['min_time']!='') $map[] = ['and','a.create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','a.create_time','<=',strtotime($d['max_time'])];
            $rs = m('OrderGoods','c')->groupLists($map,1,$d['page'],10);
            //下载
            if(isset($d['is_down']) && $d['is_down']){
                $w  = where($map);
                $sql = "select a.*,group_concat(concat_ws('',title,'(',goods_price_title,')','X',nums) separator '<br/>') as goods_title,b.username from order_goods as a
                          left join user b on a.user_id=b.id $w[0]
                          group by a.order_no order by a.id desc".limit($d['page'],$d['limit']);
                $rs_down = $this->db->query($sql, $w[1]);
                foreach ($rs_down as $k => $v) {
                    $rs_down[$k]['fahuo_name'] = $this->sys['fahuo_name'];
                    $rs_down[$k]['fahuo_phone'] = $this->sys['fahuo_phone'];
                    $rs_down[$k]['fahuo_dizhi'] = $this->sys['fahuo_dizhi'];
                    $rs_down[$k]['fahuo_youbian'] = $this->sys['fahuo_youbian'];
                    $rs_down[$k]['daishouhuokuan'] = $this->sys['daishouhuokuan'];
                    $rs_down[$k]['shoujianrenyuobian'] = $this->sys['shoujianrenyuobian'];
                    $rs_down[$k]['shoujianrenguhua'] = $this->sys['shoujianrenguhua'];
                    $rs_down[$k]['baojiajine'] = $this->sys['baojiajine'];
                }
                $title = ['物流单号','商品名称','数量','收件人姓名','收件人地址','收件人手机号','发件人姓名','发件人电话','发件人地址','发件人邮编','代收货款','备注','收件人邮编','收件人固话','保价金额'];
                $fields= ['kd_no','goods_price_title','nums','sh_name','sh_address','sh_phone','fahuo_name','fahuo_phone','fahuo_dizhi','fahuo_youbian','daishouhuokuan','kd_note','shoujianrenyuobian','shoujianrenguhua','baojiajine'];
                $file = com_csv($rs_down,$title,$fields,'ordergoods');
                cookie('js_url',$file);
            }

            if($rs){
                $rule = [
                  'pay_status' => function($v){return c("type.pay_status.{$v}");},
                  'fahuo_status' => function($v){return c("type.fahuo_status.{$v}");},
                  'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                return suc($data, $rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch(['pay_status'=>$pay_status,'fahuo_status'=>$fahuo_status]);
    }

    public function detail(){
        if(is_post()){
           $d = $this->post;
           $rule = [
               'kd_id'=>'require',
                'ids' => 'require'
           ];
           $msg = [
               'kd_id.require' => '请选择快递公司',
               'ids.require' => "请选择商品"
           ];
           check($rule,$msg,$d);
           $ids = $d["ids"];
           //快递公司
           $info = $this->db->query("select * from express where id = ?",[$d['kd_id']],1);

           $map = [
            ['and','id','in',$ids]
           ];
           $w = where($map);
           $order_no = $this->db->query("select order_no from order_goods {$w[0]}",$w[1],1);
           #更新订单记录
            $rs = $this->db->query("update order_goods set fahuo_status=1,kd_time=?,kd_no=?,kd_name=?,kd_code=?,kd_note=? where id in(".implode(',',$d["ids"]).")",[time(),$d['kd_no'],$info['name'],$info['kuaidi_no'],$d['kd_note']]);

            m('OrderGoods','c')->upfahuo($order_no['order_no']);

           if($rs){
               cookie('js_msg','发货成功');
               cookie('js_function','reload');
               return suc();
           }else{
               return err("发货失败");
           }
        }
        $order_no = $this->get("order_no");
        $map = [];
        $map[] = ['and','order_no','=',$order_no];
        $map[] = ['and','shop_id','=',$this->userinfo['id']];
        $rs = m('OrderGoods','c')->orderInfo($map);
        $sum_price = 0;
        foreach ($rs as $v) {
            $sum_price += $v['total_money'];
        }
        //快递公司列表
        $elist = $this->db->query("select * from express where status = ? order by id asc",[0]);
        $this->fetch(['rs'=>$rs,'elist'=>$elist,'sum_price'=>$sum_price]);
    }

    //下载订单
    public function downOrder(){
        if(is_post()){//下载
            $order_no = $this->post("nos");//订单编号逗号隔开
            $eid = $this->post("eid");//快递ID
            $file = m('OrderGoods','c')->downOrder($order_no,$eid);
            cookie('js_url',$file);
            cookie('js_function','openurl');
            suc();
        }
        $d = $this->get;
        $orderids = join(',',$d['ids']);
        if(empty($orderids)){
            $orderids =0;
        }
        //查询订单
        $rs = $this->db->query("select * from order_goods where id in ($orderids)",[]);
        $order_nos = join(',',array_column($rs,'order_no'));

        //快递公司列表
        $elist = $this->db->query("select * from express where status = ?",[0]);
        $this->fetch(['nos'=>$order_nos,'elist'=>$elist],'',false);
    }

}

?>
